/*
 *  Borum, Boolean rule manipulator.
 *  Copyright (C) 2009 Jon Lennersten, Johan Hedin, Daniel Rosenberg
 *
 *  This file is part of Borum.
 *
 *  Borum is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Borum is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Borum.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.mawell.borum;

import javax.swing.*;
import java.awt.*;

class MyCellRenderer extends JPanel implements ListCellRenderer {
    ImageIcon imageIcon = new ImageIcon(MultiView.class.getResource("/icons/lock_small.png"));
    JLabel nameAndIcon = new JLabel();
    JCheckBox selected = new JCheckBox();
    private boolean singleSelection;

    static Color listForeground, listBackground,
            listSelectionForeground,
            listSelectionBackground;

    static {
        UIDefaults uid = UIManager.getLookAndFeel().getDefaults();
        listForeground = uid.getColor("List.foreground");
        listBackground = uid.getColor("List.background");
        listSelectionForeground = uid.getColor("List.selectionForeground");
        listSelectionBackground = uid.getColor("List.selectionBackground");
    }

    public MyCellRenderer() {
    }

    MyCellRenderer(boolean singleSelection) {

        this.singleSelection = singleSelection;
        this.setLayout(new BorderLayout());
        add(selected, BorderLayout.WEST);
        add(nameAndIcon, BorderLayout.CENTER);
        setOpaque(true);
    }

    public boolean isSingleSelection() {
        return singleSelection;
    }

    public void setSingleSelection(boolean singleSelection) {
        this.singleSelection = singleSelection;
    }

    public Component getListCellRendererComponent(JList list,
                                                  Object value,
                                                  int index,
                                                  boolean isSelected,
                                                  boolean cellHasFocus) {

        if(((RuleView) value).isLocked()) {
            nameAndIcon.setIcon(imageIcon);
            selected.setVisible(false);
        } else {
            nameAndIcon.setIcon(null);
            if(singleSelection) {
                selected.setVisible(true);
            } else {
                selected.setVisible(false);
            }
        }
        if(((RuleView) value).isSelected()) {
            selected.setSelected(true);
        } else {
            selected.setSelected(false);
        }

        if(isSelected) {
            setForeground(listSelectionForeground);
            setBackground(listSelectionBackground);
            selected.setForeground(listSelectionForeground);
            selected.setBackground(listSelectionBackground);
        } else {
            setForeground(listForeground);
            setBackground(listBackground);
            selected.setForeground(listForeground);
            selected.setBackground(listBackground);
        }
        setForeground(Color.BLACK);

        StringBuffer text = new StringBuffer(((RuleView) value).getName().length() + 1);

        if(((RuleView) value).isEdited() || ((RuleView) value).isNewRule()) {
            text.append("*");
        }

        text.append(((RuleView) value).getName());

        if(((RuleView) value).isDeleted()) {
            setForeground(Color.GRAY);
            text.append(" (").append(((RuleView) value).getTrans().getLabelMarkedForDeletion()).append(")");
        }

        nameAndIcon.setText(text.toString());

        return this;
    }


}
